/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package daoimp;

import dao.BebidaDao;
import hibernate.HibernateUtil;
import java.util.ArrayList;
import modelo.dominio.Bebida;
import modelo.dominio.Menu;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author Pablo
 */
public class BebidaDaoImp implements BebidaDao{

    @Override
    public void insert(Bebida b) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        session.save(b);
        session.getTransaction().commit();
        session.close();
        
    }

    @Override
    public void delete(Bebida b) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        session.delete(b);
        session.getTransaction().commit();
        session.close();
        
    }

    @Override
    public void update(Bebida b) {
         Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        session.update(b);
        session.getTransaction().commit();
        session.close();
        
    }

    @Override
    public Bebida getBebida(Integer id) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        Criteria criteria = session.createCriteria(Bebida.class);
        criteria.add(Restrictions.eq("id", id));
        Bebida b = (Bebida) criteria.list().get(0);
        session.close();
        return b;
    }

    @Override
    public ArrayList<Bebida> getAll() {
       Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        Criteria criteria = session.createCriteria(Bebida.class);
        criteria.addOrder(Order.asc("id"));
        ArrayList<Bebida> list = (ArrayList<Bebida>) criteria.list();
        session.close();
        return list;
    }
    
}
